﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FriendTest.Media;
using FriendTest.Models;
using FriendTest.FriendService;

namespace FriendTest.Controllers
{
    public class MusicController : Controller
    {
        //
        // GET: /Music/
        private MediaService myService = new MediaService();
        private FriendService.Service1 myFriendService = new FriendService.Service1();
        public ActionResult MusicIndex(int page = 0)
        {
            int pagesize = 10;
            if (page * pagesize > myService.GetAllMusicInfoes().Count())
                page -= 1;
            if (page <= 0)
                page = 1;
            ViewData["page"] = page;
            MusicInfo[] model = myService.GetAllMusicInfoes();
            try
            {
                int curUserID = int.Parse(HttpContext.User.Identity.Name.Split(',')[1]);
                ViewData["Login"] = "true";
            }
            catch (System.Exception ex)
            {
                ViewData["Login"] = "false";
            }
            return View(model);
        }
        
        public ActionResult MusicInfoDetails(int resourceID)
        {
            MusicInfo music = myService.GetMusicInfo(resourceID);
            ViewData["MyTitle"] = music.title;
            ViewData["MyAuthor"] = music.author;
            ViewData["MyImageURL"] = music.imageURL;
            ViewData["MyPublisher"] = music.publisher;
            ViewData["MyPubDate"] = music.pubdate;
            ViewData["MyRatingAverage"] = music.ratingAverage;
            ViewData["MyNumRaters"] = music.numRaters;
            ViewData["MySummary"] = music.summary;
            ViewData["MyVersion"] = music.version;
            ViewData["MyTracks"] = music.tracks;
            ViewData["MyDiscs"] = music.discs;
            ViewData["MyRadioURL"] = music.radioURL;
            ViewData["MyResourceID"] = music.ResourceID;
            //int curUserID = int.Parse(HttpContext.User.Identity.Name.Split(',')[1]);
            //ViewData["CurrentUserID"] = curUserID;
            Media.Comment[] comments = myService.GetCommentList(resourceID);
            try
            {
                int curUserID = int.Parse(HttpContext.User.Identity.Name.Split(',')[1]);
                ViewData["Login"] = "true";
            }
            catch (System.Exception ex)
            {
                ViewData["Login"] = "false";
            }
            if (comments.Count() == 0)
            {
                myService.AddComment(-1, resourceID, "");
            }
            else
            {
                foreach (Media.Comment comment in comments)
                {
                    try
                    {
                        UserData ud = myFriendService.GetUserData(comment.UserID);
                        ViewData["" + comment.UserID] = ud.userName;
                    }
                    catch (System.Exception ex)
                    {
                        ViewData["" + comment.UserID] = "未知用户";
                    }
                }
            }
            comments = myService.GetCommentList(resourceID);
            return View(comments);
        }


        [Authorize]
        public ActionResult AddMusicComment(int resourceID)
        {
            MusicInfo music = myService.GetMusicInfo(resourceID);
            ViewData["MyTitle"] = music.title;
            ViewData["MyAuthor"] = music.author;
            ViewData["MyImageURL"] = music.imageURL;
            ViewData["MyPublisher"] = music.publisher;
            ViewData["MyPubDate"] = music.pubdate;
            ViewData["MyRatingAverage"] = music.ratingAverage;
            ViewData["MyNumRaters"] = music.numRaters;
            ViewData["MySummary"] = music.summary;
            ViewData["MyVersion"] = music.version;
            ViewData["MyTracks"] = music.tracks;
            ViewData["MyDiscs"] = music.discs;
            ViewData["MyRadioURL"] = music.radioURL;
            ViewData["MyResourceID"] = music.ResourceID;
            int curUserID = int.Parse(HttpContext.User.Identity.Name.Split(',')[1]);
            ViewData["CurrentUserID"] = curUserID;
            Media.Comment[] comments = myService.GetCommentList(resourceID);
            if (comments.Count() == 0)
            {
                myService.AddComment(curUserID, resourceID, "");
            }
            else
            {
                foreach (Media.Comment comment in comments)
                {
                    try
                    {
                        UserData ud = myFriendService.GetUserData(comment.UserID);
                        ViewData["" + comment.UserID] = ud.userName;
                    }
                    catch (System.Exception ex)
                    {
                        ViewData["" + comment.UserID] = "未知用户";
                    }
                }
            }
            comments = myService.GetCommentList(resourceID);
            return View(comments);
        }

        public ActionResult goToProfile(int profileID)
        {
            //////////////////////////////////////////////////////////////////////////
            return RedirectToAction("Profile", "Home", new { ID = profileID });
        }

        [AcceptVerbs(HttpVerbs.Post), ActionName("AddMusicComment")]
        public ActionResult AddCommentClick(int userID, int resourceID, string NewComment)
        {
            if (NewComment == "")
            {
                ModelState.AddModelError("NewComment", "评论不能为空");
            }
            else
            {
                myService.AddComment(userID, resourceID, NewComment);
            }
            return MusicInfoDetails(resourceID);
        }
        [AcceptVerbs(HttpVerbs.Post), ActionName("MusicIndex")]
        public ActionResult AddMusic(string SubjectIDInput)
        {
            string result = myService.AddMusic(SubjectIDInput);
            if (result == "Invalid imdb")
            {
                ModelState.AddModelError("SubjectID", "SubjectID无效");
                return MusicIndex();
            }
            else
            {
                return MusicIndex();
            }

        }

        public string ShareMusic(int resourceId)
        {
            int userId = int.Parse(HttpContext.User.Identity.Name.Split(',')[1]);
            if (!myFriendService.PublishAShare(userId, resourceId))
                return "分享失败";
            return "分享成功";
        }
    }
}
